package com.raising.modules.sys.controller;

import com.raising.framework.controller.BaseController;
import com.raising.framework.entity.ResultCode;
import com.raising.framework.entity.ResultVo;
import com.raising.framework.interceptor.Page;
import com.raising.modules.sys.entity.Organization;
import com.raising.modules.sys.entity.Role;
import com.raising.modules.sys.entity.User;
import com.raising.modules.sys.entity.enums.DataScopeEnum;
import com.raising.modules.sys.service.OrganizationService;
import com.raising.modules.sys.service.ResourceService;
import com.raising.modules.sys.service.RoleService;
import com.raising.modules.sys.service.UserService;
import com.raising.modules.sys.utils.DictUtils;
import com.raising.utils.JStringUtils;
import com.raising.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 角色权限控制器
 * @author gaoy
 */
@Controller
@RequestMapping("${adminPath}/role")
public class RoleController extends BaseController {

	@Autowired
	private RoleService roleService;
	@Autowired
	private OrganizationService organizationService;
	@Autowired
	private ResourceService resourceService;
	@Autowired
	private UserService userService;

	@ModelAttribute("dataScope")
	public DataScopeEnum[] dataScopeTypes() {
		return DataScopeEnum.values();
	}

	/**
	 * 加载列表页
	 * @author gaoyuan
	 * @return
	 */
	@RequiresPermissions("sys:role:view")
	@RequestMapping()
	public String list() {
		olog("进入角色权限管理页面");
		return "sys/role/list";
	}

	/**
	 * 获取角色权限分页数据
	 * @author GaoYuan
	 * @datetime 2018/2/23 上午10:29
	 */
	@RequiresPermissions("sys:role:view")
	@RequestMapping(value = "/getPage.json", method = RequestMethod.POST)
	public @ResponseBody ResultVo getPage(Role role, Page<Role> page) throws Exception {
		page.setEntity(role);
		page.setOrderBy(" a.sort asc ");
		return ResultVo.success().data(roleService.getPage(page));
	}

	/**
	 * 打开新增或编辑页
	 * @author gaoyuan
	 * @param id
	 * @param model
	 * @return
	 */
	@RequiresPermissions("sys:role:edit")
	@RequestMapping(value = "/update", method = RequestMethod.GET)
	public String update(String id, Model model) throws Exception{
		Role role = null;
		if(JStringUtils.isBlank(id)){
			role = new Role();
		}else{
			role = roleService.get(id);
		}
		User user = UserUtils.getLoginUser();
		Organization organization = new Organization();
		//机构列表
		model.addAttribute("organizationList", organizationService.getList(organization));
		//资源列表
		if(user.isAdmin()){
			model.addAttribute("resourceList", resourceService.getList(null));
		}else{
			User userLogin = UserUtils.getLoginUser();
			model.addAttribute("resourceList", resourceService.findRoleMenus(userService.findPermissions(userLogin.getRoleIds())));
		}
		model.addAttribute("role", role);
		 // 是否可用
        model.addAttribute("yn", DictUtils.getDictList("DIM_YN"));
		return "sys/role/edit";
	}

	/**
	 * 保存数据 ajax请求
	 * @author lirg
	 * @TrainAssess 2018/3/2 15:46
	 * @param 
	 */
	@RequiresPermissions("sys:role:edit")
	@RequestMapping(value = "/update.json", method = RequestMethod.POST)
	@ResponseBody
	public ResultVo updateRole(Role role, Model model)throws Exception {
		if(role == null || JStringUtils.hasBlank(
				role.getRole(),
				role.getSiteUrl(),
				role.getIndexUrl(),
				role.getName())){
			return ResultVo.failure(ResultCode.PARAM_EMPTY).desc("参数不合法");
		}
		if(JStringUtils.isBlank(role.getOrgId())){
			role.setOrgId("1");
		}
		if (role.getIsNewId()) {
			olog("新增角色["+role.getName()+"]");
		}else{
			olog("修改角色["+role.getName()+"]","{id:"+role.getId()+"}");
		}
		roleService.save(role);
		return ResultVo.success().desc("编辑成功").data(role.getId());
	}

	/**
	 * 删除数据
	 * @author gaoyuan
	 * @param role
	 * @return
	 */
	@RequiresPermissions("sys:role:edit")
	@RequestMapping(value = "/delete.json", method = RequestMethod.POST)
	public @ResponseBody ResultVo delete(Role role) throws Exception{
		if(!"1".equals(role.getId())){
			ResultVo resultVo = roleService.delete(role.getId());
			olog("删除角色["+role.getName()+"]","{id:"+role.getId()+"}");
			return resultVo;
		}else{
			return ResultVo.failure(ResultCode.NO_PERMISSIONS).desc("关键角色不能删除");
		}
	}

}
